package com.xzp.bams.client;

import java.awt.event.*;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.SocketException;

import com.xzp.bams.entity.Account;
import com.xzp.bams.exception.LoginException;


/**
 * @TODO
 * @Author xu
 * 2025/3/22
 */
public class LoginPanel extends JPanel {

    private ATMClient atm;

    public LoginPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    private void login(ActionEvent e) {
        //收集页面数据
        String idStr = idTextField.getText();//有问题


        String password = new String(passwordField.getPassword());
        if(idStr !=null && !idStr .equals("")){

            atm.pw.println("login#"+idStr+"#"+password);
            atm.pw.flush();

            //4.等待服务器发送的结果  读ObjectInputStream
            try {
                Object o = atm.ois.readObject();

                if(o instanceof Account){
                    //登录成功，跳转至业务界面，初始化业务界面中数据
                    Account acc = (Account) o;
                    if(acc!=null){
                        //JOptionPane.showMessageDialog(this , "登录成功");
                        //跳转至业务界面
                        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                        cardLayout.show(this.getParent(),"businessPanel");
                        //初始化业务界面中数据
                        atm.account = acc;
                        atm.businessPanel.initBusiness();
                        //清空登录界面
                        clear();
                    }
                }else if(o instanceof String){
                    //登录失败，弹框异常信息，并清空密码框
                    JOptionPane.showMessageDialog(this , o);
                    passwordField.setText("");
                }
            }  catch (SocketException ex) {
                JOptionPane.showMessageDialog(this , "服务器已关闭");
                System.exit(0);
            }  catch (IOException ex) {
                throw new RuntimeException(ex);
            }  catch (ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
            //==================加入网络编程后  end

        }else{
            JOptionPane.showMessageDialog(null,"账户不能为空！");
        }

        }

    private void cancelButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"mainPanel");
        //清空登录界面
        clear();
    }




       /* try {
            Account account = atm.bank.login(id,password);
                atm.account = account;
                atm.businessPanel.initBusiness();

                CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
                cardLayout.show(this.getParent() , "businessPanel");

                //3.清空注册界面数据
                clear();

        } catch (LoginException ex) {

            //开户失败
            //1.弹窗提醒失败信息

            JOptionPane.showMessageDialog(null , ex.getMessage());
            //2.清空密码相关的输入框
            idTextField.setText("");
            passwordField.setText("");

        }*/

//  1.发指令 2.读











    private void back(ActionEvent e) {
        // TODO add your code here
        CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
        //指定跳转至主界面
        cardLayout.show(this.getParent() , "mainPanel");
    }

    public void clear(){
        idTextField.setText("");
        passwordField.setText("");
    }

        private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        idTextField = new JTextField();
        passwordField = new JPasswordField();
        loginButton = new JButton();
        backButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- label2 ----
        label2.setText("\u767b\u5f55\u9875\u9762");
        add(label2);
        label2.setBounds(new Rectangle(new Point(155, 20), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u8d26\u53f7\uff1a");
        add(label3);
        label3.setBounds(new Rectangle(new Point(80, 50), label3.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u5bc6\u7801\uff1a");
        add(label4);
        label4.setBounds(85, 95, 38, 17);
        add(idTextField);
        idTextField.setBounds(150, 50, 135, idTextField.getPreferredSize().height);
        add(passwordField);
        passwordField.setBounds(150, 95, 140, passwordField.getPreferredSize().height);

        //---- loginButton ----
        loginButton.setText("\u767b\u5f55");
        loginButton.addActionListener(e -> login(e));
        add(loginButton);
        loginButton.setBounds(new Rectangle(new Point(90, 175), loginButton.getPreferredSize()));

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(new Rectangle(new Point(220, 180), backButton.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField idTextField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton backButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
